<template>
	<view>
		<!-- 选择收货地址盒子 -->
		<view class="address-choose-box" v-if="JSON.stringify(address)==='{}'">
			<button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>

		</view>
		<!-- 收货信息盒子 -->
		<view class="address-info-box" v-else @click="chooseAddress">
			<view class="row1">
				<view class="row1-left">
					<view class="username">收货人: <text>{{address.userName}}</text></view>
				</view>
				<view class="row1-right">
					<view class="phone">电话: <text>{{address.telNumber}}</text></view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>

			<view class="row2">
				<view class="address-tag">收货地址:</view>
				<view class="address-detail">{{addstr}}</view>
			</view>

		</view>


		<!-- 底部分割线 -->
		<image class="address-border"></image>
	</view>
</template>

<script>
	import {mapState, mapMutations,mapGetters} from 'vuex'
	export default {
		name: "my-address",
		data() {
			return {
				//地址对象
				// address: {}
			};
		},
		methods: {
			...mapMutations('m_user',['updateAddress']),
			//选择收货地址
			async chooseAddress() {
				const res = await uni.chooseAddress().catch(err => err)
				if (res.errMsg === 'chooseAddress:ok') {
					this.updateAddress(res)
					console.log(res)
				}
			}
			
			//返回值还是单个对象 所以注释了  授权现在更新了，需要配置，自动进行授权了，不需要授权提示了
			// async chooseAddress() {
			// 	// 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
			// 	// 返回值是一个数组：第 1 项为错误对象；第 2 项为成功之后的收货地址对象
			// 	const [err, succ] = await uni.chooseAddress().catch(err => err)
			// 	// 用户成功的选择了收货地址
			// 	if (err === null && succ.errMsg === 'chooseAddress:ok') {
			// 		// 为 data 里面的收货地址对象赋值
			// 		// this.address = succ
			// 		//调用 Store 中提供的 updateAddress 方法，将 address 保存到 Store 里面
			// 		// this.updateAddress(succ)
			// 		this.address = succ
			// 		// console.log(succ)
			// 	} 
			// 	// 用户没有授权
			// 	if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg ===
			// 			'chooseAddress:fail authorize no response')) {
			// 		this.reAuth() // 调用 this.reAuth() 方法，向用户重新发起授权申请
			// 	}
			// }
		},
		computed: {
			...mapState('m_user',['address']),
			//将m_user模块中的addstr映射到当前页面使用
			...mapGetters('m_user',['addstr'])
			
		}
	}
</script>

<style lang="scss">
	.address-border {
		width: 100%;
		height: 5px;


	}

	.address-choose-box {
		height: 90px;
		align-items: center;
		justify-content: center;
		display: flex;
	}

	.address-info-box {

		font-size: 12px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0 5px;
		margin-top: 10px;


		.row1 {
			display: flex;
			justify-content: space-between;

			.row1-left {}

			.row1-right {
				display: flex;
				justify-content: space-between;
			}
		}

		.row2 {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 10px;

			.address-tag {
				white-space: nowrap;
			}
		}
	}
</style>
